use tauri::{AppHandle, Emitter, Manager, WindowEvent};

#[tauri::command]
pub fn close_window(app_handle: AppHandle) {
    println!("close_window");
    app_handle.exit(0);
}

pub fn handle_window_close_requested(app_handle: AppHandle) {
    let main_window = app_handle.get_webview_window("main").unwrap();
    main_window.on_window_event(move |event| {
        if let WindowEvent::CloseRequested { api, .. } = event {
            api.prevent_close();
            let app_handle = app_handle.clone();
            tauri::async_runtime::spawn(async move {
                app_handle.emit("window-close-requested", ()).unwrap();
            });
        }
    });
}